<template>
	<view class="box">
		<uni-nav-bar class="navbar" shadow left-icon="left" @clickRight="back2" @clickLeft="back" rightIcon="closeempty"
			leftText="返回" />

		<view class="qiye">

			<uni-forms ref="valiForm" :rules="rules" :modelValue="valiFormData">
				<view class="qiye-title">
					<text>学生信息</text>
				</view>
				<view>
					<uni-forms-item labelWidth="90px" required name="xsname" label="学生姓名" labelAlign="right">
						<uni-easyinput v-model="valiFormData.xsname" placeholder="学生姓名" />
					</uni-forms-item>
				</view>
				<view>
					<uni-forms-item labelWidth="90px" required name="stunumber" label="学号" labelAlign="right">
						<uni-easyinput v-model="valiFormData.stunumber" placeholder="学号" />
					</uni-forms-item>
				</view>
				<view>
					<uni-forms-item labelWidth="90px" required name="admtime" label="入学时间" labelAlign="right">
						<uni-easyinput v-model="valiFormData.admtime" placeholder="格式应为: 1990-01" />
					</uni-forms-item>
				</view>
			</uni-forms>

			<button type="primary" @click="submit('valiForm')">提交</button>
			<button @tap="as">sss</button>
		</view>
	</view>
</template>

<script>
	import axios from "axios"
	export default {
		data() {
			return {
				// 校验表单数据
				valiFormData: {
					xsname: '',
					stunumber: '',
					admtime: '',

				},
				// 校验规则
				rules: {
					xsname: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
					stunumber: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}, {
							format: 'number',
							errorMessage: '只能输入数字'
						}]
					},
					admtime: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}, {
							validateFunction: function(rule, value, data, callback) {
								let iphoneReg = /^[1-2][0-9][0-9][0-9]-[0-1]{0,1}[0-9]$/
								if (!iphoneReg.test(value)) {
									callback('格式不正确，请重新填写')
								}
								return true
							}
						}]
					},

				},

			}
		},
		methods: {
			as(){
				const id = uni.getStorageSync('userlist')[0].id
				console.log(id);
			},
			submit(ref) {
				this.$refs[ref].validate().then(res => {
					console.log('success', res);
					uni.showToast({
						title: `校验通过`
					})
				}).then(res =>{
					const id = uni.getStorageSync('userlist')[0].id
					const xsname = this.valiFormData.xsname
					const stunumber = this.valiFormData.stunumber
					const admtime = this.valiFormData.admtime
					axios.post('/api/xsrenzheng?id='+id+'&xsname='+xsname
					+'&stunumber='+stunumber+'&admtime='+admtime)
					.then(res =>{
						console.log(res.data);
						setTimeout(() =>{
							uni.switchTab({
								url:'/pages/my/my'
							})
						}, 500);
					})
				})
				.catch(err => {
					console.log('err', err);
				});

			},

			back() {
				uni.navigateBack({
					delta: 1
				})
			},
			back2() {
				uni.switchTab({
					url: '/pages/my/my'
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.box {
		width: 100%;
		height: 100%;

		.navbar {
			width: 100%;
			height: 100%;
			position: sticky;
			top: 0;

		}
	}

	.qiye {
		width: 100%;
		height: 100%;
		padding: 5% 5% 100rpx 5%;
	}

	.qiye-title {

		padding: 5px 2%;
		background-color: #CCCCCC;
		color: #666666;
	}

	.content {
		display: flex;
		align-items: flex-end;
		padding: 10rpx;
	}

	.img {
		width: 100px;
		height: 100px;
		border: 1px solid black;
		display: flex;
		flex-direction: column;

		.img_img {
			width: 100%;
			height: 100%;
		}
	}

	.button {
		width: 300rpx;
		height: 100rpx;
		font-size: 32rpx;
		margin-left: 20rpx;
	}
</style>
